package sdkshell.kuaiyan.com.kuaiyansdk.init;

import android.Manifest;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ServiceInfo;

import com.wmcsk.dl.dl.DLProxyActivity;
import com.wmcsk.dl.dl.DLProxyFragmentActivity;
import com.wmcsk.dl.dl.DLProxyService;
import com.wmcsk.dl.dl.DLProxyServiceOne;
import com.wmcsk.dl.dl.DLProxyServiceTwo;
import com.wmcsk.dl.dl.DLReceiver;
import com.wmcsk.dl.dl.DLSingleTaskProxyActivity;
import com.wmcsk.dl.dl.DLTranslucentActivity;
import com.wmcsk.dl.dl.DLTranslucentProxyActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import sdkshell.kuaiyan.com.kuaiyansdk.PluginService;
import sdkshell.kuaiyan.com.kuaiyansdk.bean.ResponseMsgBean;
/**
 * Created by wm on 2018/1/8.
 */
public class MenifestChecker {
    public static Map<String,List<Class>> map = new HashMap<>();
    public static String reciver = "0";
    public static String activity = "1";
    public static String service = "2";
    public static String promission = "3";

    public static Map<String,String> nameMap = new HashMap<>();

    public static List<Class> reciverList = new ArrayList<>();
    public static List<Class> activitys = new ArrayList<>();
    public static List<Class> services = new ArrayList<>();

    public static List<String> promissions = new ArrayList<>();

    public static Map<String,Integer> typeMap = new HashMap<>();
    static {
        reciverList.add(DLReceiver.class);

        activitys.add(DLProxyFragmentActivity.class);
        activitys.add(DLTranslucentProxyActivity.class);
        activitys.add(DLProxyActivity.class);
        activitys.add(DLSingleTaskProxyActivity.class);

        services.add(DLProxyService.class);
        services.add(DLProxyServiceOne.class);
        services.add(DLProxyServiceTwo.class);
        services.add(PluginService.class);

        map.put(reciver,reciverList);
        map.put(activity,activitys);
        map.put(service,services);

        nameMap.put(reciver,"reciver");
        nameMap.put(activity,"activity");
        nameMap.put(service,"service");
        nameMap.put(promission,"permission");

        typeMap.put(reciver,PackageManager.GET_RECEIVERS);
        typeMap.put(activity,PackageManager.GET_ACTIVITIES);
        typeMap.put(service,PackageManager.GET_SERVICES);
        typeMap.put(promission,PackageManager.GET_PERMISSIONS);

        promissions.add(Manifest.permission.INTERNET);
        promissions.add(Manifest.permission.READ_PHONE_STATE);
        promissions.add(Manifest.permission.ACCESS_WIFI_STATE);
        promissions.add(Manifest.permission.CHANGE_WIFI_STATE);
        promissions.add(Manifest.permission.ACCESS_NETWORK_STATE);
        promissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
        promissions.add(Manifest.permission.READ_EXTERNAL_STORAGE);

    }
    public static void checkMenifest(Context context){
        ResponseMsgBean responseMsgBean = check(context,reciver);
        if(responseMsgBean == null){
            responseMsgBean = check(context,activity);
            if(responseMsgBean == null){
                responseMsgBean = check(context,service);
                if(responseMsgBean == null){
                    responseMsgBean = check(context,promission);
                }
            }
        }
        if(responseMsgBean != null){
            throw new RuntimeException(responseMsgBean.getMsg());
        }
    }


    public static ResponseMsgBean check(Context context,String flag){
        List<Class> list = map.get(flag);
        String name = nameMap.get(flag);
        PackageManager pm = context.getPackageManager();
        try {
            PackageInfo packageInfo = pm.getPackageInfo(context.getPackageName(),typeMap.get(flag));
            if(flag.equals(reciver)||flag.equals(activity)) {
                ActivityInfo activityInfo[] = flag.equals(reciver)?packageInfo.receivers:packageInfo.activities;
                for (int j = 0; j < list.size(); j++) {
                    boolean contains = false;
                    for (int i = 0; activityInfo!=null&&i < activityInfo.length; i++) {
                        if (activityInfo[i].name.equals(list.get(j).getName())) {
                            contains = true;
                            break;
                        }
                    }
                    if (!contains) {
                        ResponseMsgBean responseMsgBean = new ResponseMsgBean(-1, "请在清单文件中注册" + name + ":" + list.get(j).getName() + " 注册方式请参照sdkDemo中的注册方式");
                        return responseMsgBean;
                    }
                }
            }else if(flag.equals(service)){
                ServiceInfo serviceInfo[] = packageInfo.services;
                for (int j = 0; j < list.size(); j++) {
                    boolean contains = false;
                    for (int i = 0; serviceInfo!=null&&i < serviceInfo.length; i++) {
                        if (serviceInfo[i].name.equals(list.get(j).getName())) {
                            contains = true;
                            break;
                        }
                    }
                    if (!contains) {
                        ResponseMsgBean responseMsgBean = new ResponseMsgBean(-1, "请在清单文件中注册" + name + ":" + list.get(j).getName() + " 注册方式请参照sdkDemo中的注册方式");
                        return responseMsgBean;
                    }
                }
            }else if(flag.equals(promission)){
                String permissions[] = packageInfo.requestedPermissions;
                for(int i = 0;i<promissions.size();i++){
                    boolean contains = false;
                    for(int j = 0;permissions!=null&&j<permissions.length;j++){
                        if(permissions[j].equals(promissions.get(i))){
                            contains = true;
                        }
                    }
                    if (!contains) {
                        ResponseMsgBean responseMsgBean = new ResponseMsgBean(-1, "请在清单文件中注册" + name + ":" + promissions.get(i) + " 注册方式请参照sdkDemo中的注册方式");
                        return responseMsgBean;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
